package demo03;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author wql
 * @version 1.0
 * @date 2022/11/9 18:10
 */

public class ProxyFactory implements MethodInterceptor {
    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }

    public Object getInstance(){
        //创建一个工具---生成代理对象
        Enhancer enhancer=new Enhancer();
        //指定被代理的的父类
        enhancer.setSuperclass(target.getClass());
        //代理的回调方法
        enhancer.setCallback(this);
        return enhancer.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        watch();
        Object invoke = method.invoke(target, objects);
        tuijian();
        return invoke;
    }
    public void watch(){
        System.out.println("看房");
    }
    public void tuijian(){
        System.out.println("房源推荐");
    }
}
